<?php
session_start(); //开启session会话 类似于sessionStorage
header('Content-type:text/html;charset=utf-8');//设置编码格式
include_once 'dbconfig.php';
if ($_POST) {
    if (isset($_POST['name']) && $_POST['type'] == 'getName') {
        $name = $_POST['name'];

        $selectStr = 'select * from user where userName="' . $name . '"';
        $result = $link->query($selectStr);
        if ($result->num_rows > 0) {
            $msg = ['msg' => '已注册', 'status' => 1];
        } else {
            $msg = ['msg' => '未注册', 'status' => 0];
        }
        responseEvent($msg);
    }
    else if (isset($_POST['user']) && isset($_POST['psw']) && isset($_POST['confirm']) && isset($_POST['code'])) {
        $name = $_POST['user'];
        $psw = md5($_POST['psw']);
        $confirm = $_POST['confirm'];
        $code = $_POST['code'];
        $loginTime = time() * 1000;

        $insertStr = 'INSERT INTO `user`( `userName`, `passWord`, `loginTime`) VALUES ("' . $name . '","' . $psw . '","' . $loginTime . '")';
        /*$selectStr = 'select * from user where userName="' . $name . '"';
        $result = $link->query($selectStr);
        if ($result->num_rows > 0) {
            $msg = ['msg' => '"' . $name . '"已被注册！', 'status' => 0];
            responseEvent($msg);
        }*/
        if ($link->query($insertStr) === true) {
            $msg = ['msg' => '注册成功！', 'status' => 1];
        } else {
            $msg = ['msg' => '注册失败！', 'status' => 0];
        }
        responseEvent($msg);
    }
    else if (isset($_POST['code'])) {
        $code = strtolower($_POST['code']);
        if ($code !== strtolower($_SESSION['validate'])) {
            $msg = ['msg' => '验证码有误！', 'status' => 0];
            responseEvent($msg);
        } else {
            $msg = ['msg' => '验证码正确！', 'status' => 1];
            responseEvent($msg);
        }
    }

}
function responseEvent($msg)
{
    global $link;
    echo json_encode($msg);
    $link->close();
    exit();
}